<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    div {
      width: 200px;
      height: 200px;
      border: 1px #000000 solid;
    }
  </style>
</head>

<body>
  <input type="text " placeholder="输入你的内容">
  <button>发送请求</button>
  <div></div>
</body>
<script>
  var div = document.querySelector('div')
  var input = document.querySelector('input')
  var button = document.querySelector('button')

  //显示websocket
  var socket = new WebSocket('ws://localhost:3000')
  socket.addEventListener('open', function () {
    div.innerHTML = '连接服务成功'
  })
  button.addEventListener('click', function () {
    var value = input.value
    socket.send(value)
  })
  button.addEventListener('click', function () {
    console.log(this)
  })
  socket.addEventListener('message', function (e) {
    console.log(e.data)
    div.innerHTML = e.data
  })
  socket.addEventListener('close', function () {
    div.innerHTML = '服务断开连接了'
  })
</script>

</html>